package com.lxmes.sales.service.impl;

import java.util.List;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.bean.BeanUtils;
import com.lxmes.sales.domain.Assetfiles;
import com.lxmes.sales.mapper.AssetPdMapper;
import com.lxmes.sales.mapper.AssetfilesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.AssetDbMapper;
import com.lxmes.sales.domain.AssetDb;
import com.lxmes.sales.service.IAssetDbService;

/**
 * 资产调拨Service业务层处理
 *
 * @author cdc
 * @date 2024-08-14
 */
@Service
public class AssetDbServiceImpl implements IAssetDbService
{
    @Autowired
    private AssetDbMapper assetDbMapper;
    @Autowired
    private AssetfilesMapper assetfilesMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询资产调拨
     *
     * @param id 资产调拨主键
     * @return 资产调拨
     */
    @Override
    public AssetDb selectAssetDbById(Long id)
    {
        return assetDbMapper.selectAssetDbById(id);
    }

    /**
     * 查询资产调拨列表
     *
     * @param assetDb 资产调拨
     * @return 资产调拨
     */
    @Override
    public List<AssetDb> selectAssetDbList(AssetDb assetDb)
    {
        return assetDbMapper.selectAssetDbList(assetDb);
    }

    /**
     * 新增资产调拨
     *
     * @param assetDb 资产调拨
     * @return 结果
     */
    @Override
    public int insertAssetDb(AssetDb assetDb)
    {
        Assetfiles assetfiles = assetfilesMapper.selectAssetfilesByBarcode(assetDb.getBarcode());
        if (assetfiles==null){
            throw new ServiceException("档案代码【"+assetDb.getAssetNumber()+"】未查询到资产档案");
        }
        BeanUtils.copyProperties(assetDb,assetfiles);
        assetDb.setDeptOut(assetfiles.getSapProof());
        assetDb.setDeptIdOut(assetfiles.getReceivingUnit());
        assetDb.setCreateTime(DateUtils.getNowDate());
        assetDb.setCreateBy(SecurityUtils.getUsername());
        assetDb.setOrgCode(SecurityUtils.getFactoryCode());
        assetDb.setOrgName(SecurityUtils.getFactoryName());
        assetDb.setDbNum("1");
        assetDb.setId(null);
        String number= NewNumber.createNewNumber(assetDbMapper.getNewCode(DateUtils.getDate()));
        assetDb.setDbDanhao(number);
        return assetDbMapper.insertAssetDb(assetDb);
    }

    @Override
    public int shAssetDb(AssetDb assetDb) {
        List<Long> dbList = assetDb.getIds();
        for (Long id:dbList) {
            AssetDb db = assetDbMapper.selectAssetDbById(id);
            if (db.getShStatus().equals("1")){
                throw new ServiceException("资产调拨单号【"+db.getDbDanhao()+"】已经审核,请勿重复审核！！！");
            }
            db.setShStatus("1");
            db.setShName(SecurityUtils.getUsername());
            db.setShDate(DateUtils.getNowDate());
            assetDbMapper.updateAssetDb(db);
            //更新资产档案
            Assetfiles assetfiles = assetfilesMapper.selectAssetfilesByAssetCode(db.getAssetNumber());
            assetfiles.setSapProof(db.getDeptIn());
            assetfiles.setReceivingUnit(db.getDeptIdIn());
            assetfilesMapper.updateAssetfiles(assetfiles);
        }
        return 1;
    }

    /**
     * 修改资产调拨
     *
     * @param assetDb 资产调拨
     * @return 结果
     */
    @Override
    public int updateAssetDb(AssetDb assetDb)
    {
        return assetDbMapper.updateAssetDb(assetDb);
    }

    /**
     * 批量删除资产调拨
     *
     * @param ids 需要删除的资产调拨主键
     * @return 结果
     */
    @Override
    public int deleteAssetDbByIds(Long[] ids)
    {
        for (int i = 0; i <ids.length ; i++) {
            AssetDb db = assetDbMapper.selectAssetDbById(ids[i]);
            if (db.getShStatus().equals("1")){
                throw new ServiceException("资产调拨单号【"+db.getDbDanhao()+"】已经审核,不允许删除！！！");
            }
        }
        return assetDbMapper.deleteAssetDbByIds(ids);
    }

    /**
     * 删除资产调拨信息
     *
     * @param id 资产调拨主键
     * @return 结果
     */
    @Override
    public int deleteAssetDbById(Long id)
    {
        return assetDbMapper.deleteAssetDbById(id);
    }
    @Override
    public String imporIteminfo(List<AssetDb> assetDbList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(assetDbList) || assetDbList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AssetDb assetDb : assetDbList)
        {
            try
            {
                BeanValidators.validateWithException(validator, assetDb);
                assetDb.setCreateBy(operName);
                assetDb.setUpdateBy(operName);
                this.insertAssetDb(assetDb);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
